﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DECOR.BL.BE;
using DECOR.DL.DALC.SAPC.Proxy;
using System.Data;

namespace DECOR.DL.DALC.SAPC
{
    public class StockDALC : IStockDALC
    {
       public static StockDALC Instance() {
            if (_instance == null)
            {
                lock (typeof(StockDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new StockDALC();
                    }
                }
            }
            return _instance;
        }

        private StockDALC() { }
        private static volatile StockDALC _instance = null;


        #region IStockDALC Members

        public List<StockBE> buscarStock(StockBE pObjStock)
        {
            StockProxy proxy = new StockProxy();

            proxy.Connection = DALC.getConnection;
            if (!proxy.Connection.IsOpen) { proxy.Connection.Open(); }
            /* llamando a la función */
            ZWEB_STOCKTable tblResult = new ZWEB_STOCKTable();
            proxy.Zweb_Busca_Stock(pObjStock.CodLote, pObjStock.CodAlmacen, pObjStock.CodMaterial, pObjStock.Jerarquia, pObjStock.OrgVentas, pObjStock.CanalDist, pObjStock.CodCentro, ref tblResult);

            SAP.Connector.SAPConnectionPool.ReturnConnection(proxy.Connection);
            proxy.Connection = null;
            /* Manejando el retorno */
            DataTable dt = tblResult.ToADODataTable();

            List<StockBE> listaResult = new List<StockBE>();
            foreach (DataRow dr in dt.Rows)
            {
                StockBE obj = new StockBE();
                obj.CodMaterial = dr["matnr"].ToString();
                obj.DescMaterial = dr["matnom"].ToString();
                obj.CodCentro = dr["werks"].ToString();
                obj.DescCentro = dr["wernom"].ToString();
                obj.CodAlmacen = dr["lgort"].ToString();
                obj.DescAlmacen = dr["lgonom"].ToString();
                obj.CodLote = dr["charg"].ToString();
                obj.StockDisponible = Convert.ToDecimal(dr["stock_di"].ToString());
                obj.StockEnTransito = Convert.ToDecimal(dr["stock_cu"].ToString());
                obj.StockEntregaCliente = Convert.ToDecimal(dr["stock_ec"].ToString());
                obj.StockLibre = Convert.ToDecimal(dr["stock_lu"].ToString());
                obj.StockPedidoCliente = Convert.ToDecimal(dr["stock_pc"].ToString());

                //obj.sStockDisponible = (Convert.ToDecimal(dr["stock_di"].ToString()) > 500) ? "> 500" : dr["stock_di"].ToString();

                listaResult.Add(obj);
            }
            return listaResult;
        }

        public List<StockBE> buscarStockEC(StockBE pObjStock)
        {
            StockProxy proxy = new StockProxy();
            proxy.Connection = DALC.getConnection;
            if (!proxy.Connection.IsOpen) { proxy.Connection.Open(); }
            /* llamando a la función */
            ZWEB_STOCK_ECTable tblResult = new ZWEB_STOCK_ECTable();
            if  (pObjStock.Jerarquia==null) 
                 pObjStock.Jerarquia= "";


            proxy.Zweb_Busca_Stock_Ec(pObjStock.CodLote, pObjStock.CodAlmacen, pObjStock.CodMaterial, pObjStock.Jerarquia, pObjStock.CodCentro, ref tblResult);

            SAP.Connector.SAPConnectionPool.ReturnConnection(proxy.Connection);
            proxy.Connection = null;
            /* Manejando el retorno */
            DataTable dt = tblResult.ToADODataTable();
            List<StockBE> listaResult = new List<StockBE>();
            foreach (DataRow dr in dt.Rows)
            {
                StockBE obj = new StockBE();
                obj.CodMaterial = dr["matnr"].ToString();
                obj.DescMaterial = dr["maktx"].ToString();
                obj.CodCentro = dr["werks"].ToString();
                obj.DescCentro = dr["name1"].ToString();
                obj.CodAlmacen = dr["lgort"].ToString();
                obj.DescAlmacen = dr["lgobe"].ToString();
                obj.CodLote = dr["charg"].ToString();
                obj.StockDisponible = Convert.ToDecimal(dr["cdisp"].ToString());
                obj.StockEnTransito = Convert.ToDecimal(dr["menge"].ToString());
                if (dr["eindt"].ToString() != "" && dr["eindt"].ToString() != "00000000")
                {
                    obj.FechaStock = ParametroDALC.dateSapToNet(dr["eindt"].ToString());
                }
                obj.UMB = dr["meins"].ToString();

                //obj.sStockDisponible = (Convert.ToDecimal(dr["cdisp"].ToString()) > 500) ? "> 500" : dr["cdisp"].ToString();

                listaResult.Add(obj);
            }
            return listaResult;
        }

        #endregion

        public List<StockCursoBE> buscaStockECTRansito(string numMat, string matDesc, string numDoc, DateTime fechaIni, DateTime fechaFin)
        {
            throw new NotImplementedException();
        }
    }
}
